<!-- 新增和编辑弹窗 -->
<template>
  <a-modal
    :width='800'
    :visible='visible'
    :confirm-loading='loading'
    :title="isUpdate ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible='updateVisible'
    @ok='save'
  >
    <a-form
      layout='horizontal'
      ref='form'
      :model='form'
      :rules='rules'
      :label-col='{ md: { span: 4 }, sm: { span: 24 } }'
      :wrapper-col='{ md: { span: 20 }, sm: { span: 24 } }'
    >

      <a-form-item label='继电器ID:' name='relayid'>
        <a-input v-model:value='form.relayid' placeholder='请输入继电器ID' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label="设备类型:" name="deviceType">
        <a-select
          show-search
          v-model:value="form.deviceType"
          placeholder="请选择设备类型"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
        >
          <a-select-option v-for="item in devicelist" :key="item.templateCode" >
            {{ item.templateName }}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { DeviceSprayCameraWifirelayApi } from '@/api/property/spraycamera/DeviceSprayCameraWifirelayApi';
import { message } from 'ant-design-vue';
import {DeviceTemplateApi} from "@/api/property/DeviceTemplateApi";

export default {
  name: 'DeviceSprayCameraWifirelayEdit',
  components: {
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  created() {
    if (this.data) {
      this.form = Object.assign({}, this.data);
      this.isUpdate = true;
    } else {
      this.form = {};
      this.isUpdate = false;
    }
  },
  async mounted() {
    // 查询菜单节点
    let deviceList = await DeviceTemplateApi.list({})
    this.devicelist = (deviceList ? deviceList.data : null) || [];
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        deviceType: [{ required: true, message: '请输入设备类型', type: 'string', trigger: 'blur' }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      devicelist : [],
    };
  },
  methods: {
    /**
     * 保存和编辑
     *
     * @author cancan
     * @date 2025/03/02 22:29
     */
    async save() {
      console.log(this.form);
      // 校验表单
      await this.$refs.form.validate();

      // 修改加载框为正在加载
      this.loading = true;

      let result = null;
      // 执行编辑或修改
      if (this.isUpdate) {
        let updateForm = {
          "scwId" : this.form.scwId,
          "deviceType" : this.form.deviceType
        };
        result = DeviceSprayCameraWifirelayApi.edit(updateForm);
      } else {
        result = DeviceSprayCameraWifirelayApi.add(this.form);
      }
      result
        .then(result => {
          // 移除加载框
          this.loading = false;

          // 提示添加成功
          message.success(result.message);

          // 如果是新增，则form表单置空
          if (!this.isUpdate) {
            this.form = {};
          }

          // 关闭弹框，通过控制visible的值，传递给父组件
          this.updateVisible(false);

          // 触发父组件done事件
          this.$emit('done');
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /**
     * 更新编辑界面的弹框是否显示
     *
     * @param value true-显示，false-隐藏
     * @author cancan
     * @date 2025/03/02 22:29
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
};
</script>
